﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BUS;
using DTO;

public partial class Details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        String IDSanPham = Request.QueryString["itemID"];
        if (Request.QueryString["IDComment"] != null)
        {
            int idComment = Int16.Parse(Request["IDComment"]);
            BUS.NhanXetBus.XoaNhanXetTheoID(idComment);
        }

        SanPhamDto sanpham = SanPhamBus.LaySanPhamTheoID(int.Parse(IDSanPham));

        // Xuat thong tin
        TenSanPham.Text = sanpham.TenSanPham;
        Image.ImageUrl = sanpham.Hinh;
        soluong.Text = sanpham.SoLuongCon.ToString();
        if (sanpham.SoLuongCon > 0)
            Gio.NavigateUrl = "Cart.aspx?itemID=" + IDSanPham.ToString();
        else
            Gio.Visible = false;
        if (sanpham.IDKhuyenMai != 4)
        {
            KhuyenMaiDto khuyenmai = KhuyenMaiBus.LayKhuyenMaiTheoID(sanpham.IDKhuyenMai);
            sanpham.IDGiamGia = sanpham.DonGia * (100 - khuyenmai.HinhThuc) / 100;
            Tinhtrang.Text = "Hang giam gia";
            GiaCu.Text = sanpham.DonGia.ToString() + ".000 VND";
            GiaGiam.Text = sanpham.IDGiamGia.ToString() + ".000 VND";
        }
        else
        {
            if (sanpham.SoLuongCon > 0)
                Tinhtrang.Text = "Con hang";
            else
                Tinhtrang.Text = "Het hang";

            Gia.Text = sanpham.DonGia.ToString() + "$";
        }
        Rating1.CurrentRating = sanpham.DiemBinhChon;

        List<DTO.NhanXetDto> lstComment = BUS.NhanXetBus.LayNhanXetCuaSanPham(int.Parse(IDSanPham));
        if (lstComment.Count == 0)
        {
            Label5.Text = "Nhận xét của khách hàng: <span class='invalidMessage'>Hiện chưa có nhận xét nào về sản phẩm này</span>";
        }
        else
        {
            DataNhanXet.DataSource = lstComment;
            DataNhanXet.DataBind();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        String commentContent = txtCommentContent.Text;

        if (commentContent == "")
        {
            lblSecureCodeErr.Text = "";
            lblContentErr.Text = "<span class='invalidMessage'>Nội dung không được để trống</span>";
            return;
        }

        sercureCode.ValidateCaptcha(txtConfirmSecureCode.Text);
        if (!sercureCode.UserValidated)
        {
            lblContentErr.Text = "";
            lblSecureCodeErr.Text = "<span class='invalidMessage'>Mã an toàn không chính xác</span>";
            return;
        }

        DTO.NguoiDungDto user = (DTO.NguoiDungDto)Session["user"];
        DTO.NhanXetDto newComment = new NhanXetDto();
        newComment.IDNguoiDung = user.Ma;
        newComment.MaSanPham = Int16.Parse(Request["itemID"]);
        newComment.NoiDung = commentContent;
        newComment.ThoiDiem = DateTime.Now;
        newComment.TenNguoiDung = user.TenDangNhap;

        BUS.NhanXetBus.ThemNhanXet(newComment);

        lblContentErr.Text = "";
        lblSecureCodeErr.Text = "";
        txtCommentContent.Text = "";
        txtConfirmSecureCode.Text = "";

        Response.Redirect("Details.aspx?itemID=" + newComment.MaSanPham);
    }
    protected void Rating1_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
    {
        String IDSanPham = Request.QueryString["itemID"];
        SanPhamDto sanpham = SanPhamBus.LaySanPhamTheoID(int.Parse(IDSanPham));

        sanpham.DiemBinhChon = (sanpham.DiemBinhChon * sanpham.Solandanhgia + Rating1.CurrentRating) / (sanpham.Solandanhgia + 1);
        sanpham.Solandanhgia += 1;

        SanPhamBus.CapnhatDiemSanPham(sanpham.DiemBinhChon, sanpham.Solandanhgia, sanpham.ID);
    }
}
